package gla.centralhotel.business;

import gla.centralhotel.dao.interfaces.IAgencyDAO;
import gla.centralhotel.dao.interfaces.IManagerDAO;
import gla.centralhotel.exception.AgencySubscriptionException;
import gla.centralhotel.exception.IllegalAuthenticationException;
import gla.centralhotel.exception.ManagerSubscriptionException;
import gla.centralhotel.exception.NoUserFoundException;
import gla.centralhotel.exception.SubscriptionManagementException;
import gla.centralhotel.model.Agency;
import gla.centralhotel.model.Country;
import gla.centralhotel.model.Manager;
import gla.centralhotel.model.Zipcode;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author Rivo
 */
@Stateless
public class SubscriptionManagement implements ISubscriptionManagement {

    @EJB
    private IAgencyDAO agencyDAO;
    @EJB
    private IManagerDAO managerDAO;

    /**
     * Ajoute un "manager" d'hotel
     * @param manager
     * @return
     */
    public Manager saveManagerSubscription(Manager manager) throws SubscriptionManagementException, IllegalAuthenticationException {
        if (manager.getEmail() != null && manager.getPassword() != null) {
            int length = manager.getEmail().length();
            int pwdSize = manager.getPassword().length();
            try {
                if (manager.getEmail().split("@").length == 2 && (10 <= length && length <= 80) && (5 <= pwdSize && pwdSize <= 20)) {
                    return managerDAO.saveManager(manager);
                } else {
                    throw new IllegalAuthenticationException("E-mail incorrect!!");
                }
            } catch (IllegalAuthenticationException ex) {
                Logger.getLogger(SubscriptionManagement.class.getName()).log(Level.SEVERE, null, ex);
                throw new IllegalAuthenticationException("Login et mot de passe incorrectes!!");
            } catch (ManagerSubscriptionException ex) {
                Logger.getLogger(SubscriptionManagement.class.getName()).log(Level.SEVERE, "Manager hotel existe deja", ex);
                throw new SubscriptionManagementException("E-mail déjà présent dans le système");
            }
        } else {
            throw new IllegalAuthenticationException("E-mail ou password null !!");
        }
    }

    public Agency saveAgencySubscription(Agency agency) throws AgencySubscriptionException, IllegalAuthenticationException {
        if (agency.getLogin() != null && agency.getPassword() != null) {
            int length = agency.getLogin().length();
            int pwdSize = agency.getPassword().length();
        try {
            if (agency.getLogin().split("@").length == 2 && (10 <= length && length <= 80) && (5 <= pwdSize && pwdSize <= 20)) {
                    return agencyDAO.saveAgency(agency);
            } else {
                throw new IllegalAuthenticationException("E-mail incorrect!!");
            }
        } catch (AgencySubscriptionException ex) {
            Logger.getLogger(SubscriptionManagement.class.getName()).log(Level.SEVERE, null, ex);
            throw new AgencySubscriptionException("E-mail déjà présent dans le système");
        } catch (IllegalAuthenticationException ex) {
            Logger.getLogger(SubscriptionManagement.class.getName()).log(Level.SEVERE, null, ex);
            throw new IllegalAuthenticationException("Login et mot de passe incorrectes!!");
        }
    }else{
           throw new IllegalAuthenticationException("E-mail incorrect!!"); 
    }
    }

    /**
     * Le manager ne contient pas directement les refZipcode et refCountry
     * @param manager
     * @param zipcode
     * @param country
     * @return
     */
    public Manager saveManagerSubscription(Manager manager, Zipcode zipcode, Country country) throws SubscriptionManagementException, IllegalAuthenticationException {
        manager.setRefZipcode(zipcode);
        manager.setRefCountry(country);
        if (manager.getEmail() != null && manager.getPassword() != null) {
            int length = manager.getEmail().length();
            int pwdSize = manager.getPassword().length();
            try {
                if (manager.getEmail().split("@").length == 2 && (10 <= length && length <= 80) && (5 <= pwdSize && pwdSize <= 20)) {
                    return managerDAO.saveManager(manager);
                } else {
                    throw new IllegalAuthenticationException("E-mail incorrect!!");
                }
            } catch (ManagerSubscriptionException ex) {
                Logger.getLogger(SubscriptionManagement.class.getName()).log(Level.SEVERE, null, ex);
                throw new SubscriptionManagementException("E-mail déjà présent dans le système");
            } catch (IllegalAuthenticationException ex) {
                Logger.getLogger(SubscriptionManagement.class.getName()).log(Level.SEVERE, null, ex);
                throw new IllegalAuthenticationException("Login et mot de passe incorrectes!!");
            }
        } else {
            throw new IllegalAuthenticationException("E-mail ou password null !!");
        }
    }

    /**
     * Màj d'un manager d'hotel
     * @param manager
     * @return
     * @throws gla.centralhotel.exception.ManagerSubscriptionException
     * @throws gla.centralhotel.exception.IllegalAuthenticationException
     */
    public Manager updateManagerInfo(Manager manager) throws ManagerSubscriptionException, IllegalAuthenticationException {
        if (manager.getEmail() != null && manager.getPassword() != null) {
            int length = manager.getEmail().length();
            int pwdSize = manager.getPassword().length();
            try {
                if (manager.getEmail().split("@").length == 2 && (10 <= length && length <= 80) && (5 <= pwdSize && pwdSize <= 20)) {
                    return managerDAO.updateManager(manager);
                } else {
                    throw new IllegalAuthenticationException("E-mail incorrect!!");
                }
            } catch (ManagerSubscriptionException ex) {
                throw new ManagerSubscriptionException("Mise à jour impossible les " +
                        "infos suivantes sont incorrectes : id et/ou email et/ou mote de passe");
            } catch (IllegalAuthenticationException e) {
                throw new IllegalAuthenticationException("Erreur de mise à jour :" +
                        " id en base est différent de celui à mettre à jour");
            }
        } else {
            throw new IllegalAuthenticationException("E-mail ou password null !!");
        }
    }
}
